<?php
namespace common\component;

use common\models\User;
use common\service\UserService;
use yii\web\Controller;
use Yii;
use yii\web\HttpException;
use common\models\Setting;

class BaseWebController extends Controller
{
    public $enableCsrfValidation = false;
	public $modelClass = 'common\models\Setting';
	/**
	 * 系统配置
	 * @var array
	 */
	public $_setting = array();
	public $_data = array();

	//初始化系统配置信息
	public function init()
	{
		parent::init();
		$this->_setting = Setting::getSetting();
		Yii::$app->params['setting'] = $this->_setting;

		/**
		 *  配置信息获取方法
		 *  1、继承了该控制器的  $this->_setting['domain'];
		 *  2、全局使用         Yii::$app->params['setting']['domain'];
		 */
	}

	//获得用户UID
	public function getUid()
	{
		return \Yii::$app->user->identity->id;
	}

	//获取post参数
	public function post($key = "", $default = "") {
		if(empty($key)){
			return \Yii::$app->request->post();
		}
		return \Yii::$app->request->post($key, $default);
	}

	//获取get参数
	public function get($key = "", $default = "") {
		if(empty($key)){
			return \Yii::$app->request->get();
		}
		return \Yii::$app->request->get($key, $default);
	}

	//判断是否ajax提交
	public function _isAjax()
	{
		return \Yii::$app->request->isAjax;
	}

	//判断是否post提交
	public function _isPost()
	{
		return \Yii::$app->request->isPost;
	}

	//判断是否get提交
	public function _isGet()
	{
		return \Yii::$app->request->isGet;
	}


	/**
	 * @desc    渲染视图
	 * @access  public
	 * @param   string $action 视图名称
	 * @return  void
	 */
	public function view($action = null)
	{
		if (is_null($action)) $action = strtolower(\Yii::$app->controller->action->id);
		return $this->render($action, $this->_data);
	}

	//设置cookie
	protected function setCookie($name,$value,$expire = 0){
		$cookies = \Yii::$app->response->cookies;
		$cookies->add( new \yii\web\Cookie([
			'name' => $name,
			'value' => $value,
			'expire' => $expire
		]));
	}

	//删除cookie
	protected  function getCookie($name,$default_val=''){
		$cookies = \Yii::$app->request->cookies;
		return $cookies->getValue($name, $default_val);
	}

	//删除cookie
	protected function removeCookie($name){
		$cookies = \Yii::$app->response->cookies;
		$cookies->remove($name);
	}

	//返回json数据
	protected function renderJSON($data=[], $msg ="ok", $code = 200)
	{
		header('Content-type: application/json');
		echo json_encode([
			"code"   => $code,
			"msg"    =>  $msg,
			"data"   =>  $data,
			"req_id" =>  uniqid()
		]);
		return \Yii::$app->end();
	}

	//统一js提醒
	protected  function renderJS($msg,$url = "/")
	{
		return $this->renderPartial("@proxy/views/common/js", ['msg' => $msg, 'location' => $url]);
	}


	/**
	 * ---------------------------------------
	 * 修改数据表一条记录的一条值
	 * @param \yii\db\ActiveRecord $model 模型名称
	 * @param array  $data 数据
	 * @return \yii\db\ActiveRecord|boolean
	 * ---------------------------------------
	 */
	public function saveRow($model, $data){
		if (empty($data)) {
			return false;
		}
		if ($model->load($data, '') && $model->validate()) {
			/* 添加到数据库中,save()会自动验证rule */
			if ($model->save()) {
				return $model;
			}else{
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * ---------------------------------------
	 * 由表主键删除数据表中的多条记录
	 * @param \yii\db\ActiveRecord $model 模型名称,供M函数使用的参数
	 * @param string  $pk  修改的数据
	 * @return boolean
	 * ---------------------------------------
	 */
	public function delRow($model, $pk='id' ){
		$ids = Yii::$app->request->param($pk, 0);
		$ids = implode(',', array_unique((array)$ids));

		if ( empty($ids) ) {
			return false;
		}

		$_where = $pk.' in('.$ids.')';
		if($model::deleteAll($_where)){
			return true;
		} else {
			return false;
		}
	}

}


